using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Favorites
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Favorites : SqlServerDAO
    {
        #region Favorites Private Fields

		private Int32 _id;
		private Int32 _productId;
		private DateTime _whenAdded;
		private Int32 _forUserId;

        #endregion

        #region Favorites Constractors

		public Favorites(){}

		public Favorites
		(
			Int32 id,
			Int32 productId,
			DateTime whenAdded,
			Int32 forUserId

		)
		{
			this._id = id;
			this._productId = productId;
			this._whenAdded = whenAdded;
			this._forUserId = forUserId;
		}

        #endregion

        #region Favorites Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public Int32 ProductId
		{
			get{ return this._productId; }
			set{ this._productId = value; }
		}

		public DateTime WhenAdded
		{
			get{ return this._whenAdded; }
			set{ this._whenAdded = value; }
		}

		public Int32 ForUserId
		{
			get{ return this._forUserId; }
			set{ this._forUserId = value; }
		}


        #endregion

        #region Favorites Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewFavorites
        (
			Int32 productId,
			DateTime whenAdded,
			Int32 forUserId

        )
        {
            //creating an empty Favorites object
            Favorites favorites = new Favorites();
            favorites.ProductId = productId;
			favorites.WhenAdded = whenAdded;
			favorites.ForUserId = forUserId;

            return Favorites.CreateNewFavorites(favorites);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateFavorites
        (
			Int32 id,
			Int32 productId,
			DateTime whenAdded,
			Int32 forUserId

        )
        {
            Favorites favorites = Favorites.GetFavoritesById(id);
            favorites.ProductId = productId;
			favorites.WhenAdded = whenAdded;
			favorites.ForUserId = forUserId;

            return Favorites.UpdateFavorites(favorites);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteFavorites
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Favorites"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_DELETEFAVORITES);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteFavoritesList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Favorites"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_DELETEFAVORITESLIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<Favorites> GetFavoritesByProductId(Int32 productId)
		{
			if(productId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("productId in Favorites"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@ProductId", SqlDbType.Int, 0, ParameterDirection.Input, productId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_GETFAVORITESBYPRODUCTID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateFavoritesCollectionFromReader);
			CustomCollection<Favorites> objCollection = ((CustomCollection<Favorites>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<Favorites> GetFavoritesByForUserId(Int32 forUserId)
		{
			if(forUserId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("forUserId in Favorites"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@ForUserId", SqlDbType.Int, 0, ParameterDirection.Input, forUserId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_GETFAVORITESBYFORUSERID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateFavoritesCollectionFromReader);
			CustomCollection<Favorites> objCollection = ((CustomCollection<Favorites>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

        #endregion

        #region Favorites Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewFavorites(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateFavorites(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Favorites> GetAllFavorites()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_GETALLFAVORITES);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateFavoritesCollectionFromReader);
            CustomCollection<Favorites> objCollection = ((CustomCollection<Favorites>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Favorites> GetAllFavoritesPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_GETALLFAVORITESPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateFavoritesCollectionFromReader);
            CustomCollection<Favorites> objCollection = ((CustomCollection<Favorites>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllFavoritesPagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_GETALLFAVORITESPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Favorites GetFavoritesById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Favorites"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_GETFAVORITESBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateFavoritesCollectionFromReader);
            CustomCollection<Favorites> objCollection = ((CustomCollection<Favorites>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Favorites)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewFavorites(Favorites newFavorites)
        {
            // validate Parameters
            if (newFavorites == null)
                throw (new ArgumentNullException("newFavorites in Favorites"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@ProductId", SqlDbType.Int, 0, ParameterDirection.Input, newFavorites.ProductId);
			AddParameterToSqlCmd(sqlCmd, "@WhenAdded", SqlDbType.DateTime, 0, ParameterDirection.Input, newFavorites.WhenAdded);
			AddParameterToSqlCmd(sqlCmd, "@ForUserId", SqlDbType.Int, 0, ParameterDirection.Input, newFavorites.ForUserId);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_CREATENEWFAVORITES);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateFavorites(Favorites newFavorites)
        {
            // validate Parameters
            if (newFavorites == null)
                throw (new ArgumentNullException("newFavorites"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newFavorites.Id);
            AddParameterToSqlCmd(sqlCmd, "@ProductId", SqlDbType.Int, 0, ParameterDirection.Input, newFavorites.ProductId);
			AddParameterToSqlCmd(sqlCmd, "@WhenAdded", SqlDbType.DateTime, 0, ParameterDirection.Input, newFavorites.WhenAdded);
			AddParameterToSqlCmd(sqlCmd, "@ForUserId", SqlDbType.Int, 0, ParameterDirection.Input, newFavorites.ForUserId);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_FAVORITES_UPDATEFAVORITES);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Favorites Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Favorites custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Favorites information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateFavoritesCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Favorites collection
            CustomCollection<Favorites> colFavorites = new CustomCollection<Favorites>();

            //Iterating thriugh the data reader, to generate Favorites collection.

            //getting the Favorites collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Favorites constructor parameters from the current instance of data reader fields.
                Favorites newFavorites = new Favorites
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["product_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["product_id"],
					returnData["when_added"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["when_added"],
					returnData["for_user_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["for_user_id"]

                );
                //adding the Favorites to the collection
                colFavorites.Add(newFavorites);
            }

            //returns the collection of Favorites 
            return (colFavorites);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_FAVORITES_CREATENEWFAVORITES = "spr_Favorites_CreateNewFavorites";
        internal const string SPR_FAVORITES_UPDATEFAVORITES = "spr_Favorites_UpdateFavorites";
        internal const string SPR_FAVORITES_DELETEFAVORITES = "spr_Favorites_DeleteFavorites";
        internal const string SPR_FAVORITES_DELETEFAVORITESLIST = "spr_Favorites_DeleteFavoritesList";
        internal const string SPR_FAVORITES_GETFAVORITESBYID = "spr_Favorites_GetFavoritesById";
        internal const string SPR_FAVORITES_GETALLFAVORITES = "spr_Favorites_GetAllFavorites";
        internal const string SPR_FAVORITES_GETALLFAVORITESPAGED = "spr_Favorites_GetAllFavoritesPaged";
        internal const string SPR_FAVORITES_GETALLFAVORITESPAGEDCOUNT = "spr_Favorites_GetAllFavoritesPagedCount";

		//Foreign key contants
		internal const string SPR_FAVORITES_GETFAVORITESBYPRODUCTID="spr_Favorites_GetFavoritesByProductId";
		internal const string SPR_FAVORITES_GETFAVORITESBYPRODUCTIDPAGED="spr_Favorites_GetFavoritesByProductIdPaged";
		internal const string SPR_FAVORITES_GETFAVORITESBYPRODUCTIDPAGEDCOUNT="spr_Favorites_GetFavoritesByProductIdPagedCount";
		internal const string SPR_FAVORITES_GETFAVORITESBYFORUSERID="spr_Favorites_GetFavoritesByForUserId";
		internal const string SPR_FAVORITES_GETFAVORITESBYFORUSERIDPAGED="spr_Favorites_GetFavoritesByForUserIdPaged";
		internal const string SPR_FAVORITES_GETFAVORITESBYFORUSERIDPAGEDCOUNT="spr_Favorites_GetFavoritesByForUserIdPagedCount";
		
        #endregion
    }
}
